﻿package org.kolonitsky.alexey.banner.texteffect
{
	import flash.events.Event;
	import flash.display.Shape;
	import flash.display.MovieClip;
	import flash.display.DisplayObject;
	import flash.text.StaticText;
	
    /**
     *  Simple class for collect basic propramaticly animation action.
     *  For use this class add Tweener library to your project.
     * 
     *  Show text or graphics has used <code>init</code> method.
     *  Hide text has used <code>remove</code>
     *
     *  Movie clip which use this class must define functions
     *  <code>
     *  addClip(movieClip, order, index);
     *  removeClip(movieClip, order, index);
     * </code>
     */
	public class BaseTextEffect extends MovieClip
	{
		
		public var order:Array;
		
		/**
		 * Constructor
		 */
		public function BaseTextEffect()
		{
			super();
			order = [];
		}
		
		
		/**
		 *  init function collect all moveclip child, convert them to 
         *  MovieClip objects and run <code>addClip</code> function 
         *  for each clip.
         * 
         *  @param orderFunc passed for change default text order. It can 
         *  be any function with one array parameter for modify passed array.
         *  Default function randomOrder, reverseOrder
		 */
		public function init(orderFunc:Function=null):void
		{
			// fill array
			for (var i:int = 0; i < numChildren; i++)
				order[i] = i;
			
			if (orderFunc != null)
				orderFunc(order);
			
			for (i=0; i < numChildren; i++) 
			{
				var mc:MovieClip;
				
				var child:DisplayObject = getChildAt(order[i]);
                if (child is Shape)
                {
                    var shape:Shape = Shape(child);
                    removeChild(shape);
                    mc = new MovieClip();
                    mc.x = shape.x;
                    mc.y = shape.y;
                    shape.x = 0;
                    shape.y = 0;
                    mc.addChild(shape);
                    addChildAt(mc, order[i]);
                }
				else if (child is StaticText)
				{
					var tf: StaticText = StaticText(child);
					removeChild(tf);
					mc = new MovieClip();
					mc.x = tf.x;
					mc.y = tf.y;
					tf.x = 0;
					tf.y = 0;
					mc.addChild(tf);
					addChildAt(mc, order[i]);
				}
				else if (child is MovieClip) 
				{
					mc = MovieClip(child);
				}
				else {
					removeChild(child);
				}
				
				this["addClip"](mc, order[i], i);
			}
		}
		
		/**
		 *  Hide all elements.
		 */
		public function remove():void
		{
			var n:uint = order.length;
			for (var i:int=0; i < n; i++) 
			{
				this["removeClip"](MovieClip(getChildAt(order[i])), order[i], i);
			}
		}
		
		
		/**
		 *
		 */
		public function randomOrder(ar:Array):void
		{
			var n:uint = ar.length;
			for (var i:int=0; i < n; i++) 
			{
				var ind: int = int(Math.random() * n);
				var xch: int = ar[ind];
				ar[ind] = ar[i];
				ar[i] = xch;
			}
		}
		
		
		/**
		 *
		 */
		public function reversOrder(ar:Array):void 
		{
			ar = ar.reverse();
		}

	}
}